home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7706 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  41 lines

  1. Path: proffa.cc.tut.fi!m151843
  2. From: m151843@proffa.cc.tut.fi (Edvard Majakari)
  3. Newsgroups: comp.lang.c++
  4. Subject: Yet a question about random
  5. Date: 25 Feb 1996 12:47:24 GMT
  6. Organization: Heart of Gold, Milk-O-Matic & Bicep-O-Bulgizer
  7. Distribution: world
  8. Message-ID: <4gplos$lds@cc.tut.fi>
  9. NNTP-Posting-Host: proffa.cc.tut.fi
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. I use the following function to produce (pseudo)random numbers
  13. between 1...top. However, this algorithm seems to be far from
  14. good pseudorandom. But the hint was in rand manpage - any good
  15. ideas to produce better 'random' numbers?
  16.  
  17. -----&<------------------------&<-------------------------------
  18.  
  19. #include <stdlib.h>
  20. #include <sys/time.h>
  21. #include <unistd.h>
  22.  
  23. int randomize(float top) {
  24.     int rnum;
  25.     timeval seed;  //see struct timeval for further details
  26.     gettimeofday(&seed, NULL);
  27.     srand(seed.tv_usec); //initialize with millisecs
  28.     rnum = 1+(int) (top*rand()/(RAND_MAX+1.0));
  29.     return rnum;
  30. }
  31.  
  32. -----&<------------------------&<-------------------------------
  33. --
  34. //Ed            http://lodge.ton.tut.fi/~ed/
  35. %  
  36. Fact : I ache , therefore I am...or in my case I am therefore I ache.
  37.        It reminds me of that thing, what was it Oh yes...life......maybe
  38.        if I just lie down and ignore it, it will go away....or perhaps
  39.        not..... Zootlewerdle zootlewerdle zootlewerdle (Marvin Lying in a
  40.        deep dark hole). -- from HHGTTG
  41.